<?php $this->_extends('_layouts/main_layout'); ?>
<?php $this->_block('title'); ?>
 Users Search
<?php $this->_endblock(); ?>

<?php $this->_block('contents'); ?>
<div id="content" class="box">
	<h1>Users Search</h1>
		<?php if (isset($infos)) 
				{
					foreach ($infos as $info)
					{
		?>
				<p class="msg info"><?php echo $info;?></p>
		<?php		}
				}
		?>
	<fieldset>
		<legend>Search</legend>
		<form action="<?echo url('user/search'); ?>" method="get">
			<div class="row" id="search">
				<select name="field">
                    <option value="email" <?php echo ('email'==$field) ? 'selected="selected"' : ''; ?>>Email</option>
					<option value="id" <?php echo ('id'==$field) ? 'selected="selected"' : ''; ?>>ID</option>
					<option value="firstname" <?php echo ('firstname'==$field) ? 'selected="selected"' : ''; ?>>First Name</option>
					<option value="lastname" <?php echo ('lastname'==$field) ? 'selected="selected"' : ''; ?>>Last Name</option>
				</select>
				<input type="text" name="condition" value="<?php echo isset($condition) ? $condition : ''; ?>"/>
				<input type="submit" value="Search" />
			</div>
		</form>
	</fieldset>
	<div class="row"><a href="<?php echo url('user/advancedsearch');?>">Advanced Search</a></div>
	<table class="full-size">
		<tbody>
			<tr>
				<th>ID</th>
				<th>Domain</th>
				<th>Name</th>
				<th>State</th>
				<th>Won</th>
				<th>Balance</th>
				<th>Last Visit</th>
			</tr>
			<?php
				$bg = false;
				if(!empty($users)){
					foreach ($users as $user){
			?>
			<tr <?php if ($bg) echo 'class="bg"';?>>
				<td><label style="text-decoration:underline; color:#06F; cursor:pointer;" class='un'><?php echo $user['id'];?></label></td>
				<td><?php echo $user['domain'];?></td>
				<td><?php echo $user['fistname'] . $user['lastname'];?></td>
				<td><?php echo $user['state']; ?></td>
				<td><?php echo $user['won']; ?></td>
				<td><?php echo $user['balance']; ?></td>
				<td><?php echo date('m/d/Y H:i:s', $user['last_visit']); ?></td>
			</tr>
			<?php
					$bg = !$bg;
					}
				}
			?>
		</tbody>
	</table>
	<div class="pagination">
		<?php echo $pagination; ?>
	</div>

   	<table id='userinfo'>
        <tr>
            <th>Name:</th>
            <td id='userinfo_name'></td>
        </tr>
        <tr>
            <th>Email:</th>
            <td id='userinfo_email'></td>
        </tr>
        <tr>
            <th>Address1:</th>
            <td id='userinfo_addr1'></td>
        </tr>
        <tr>
            <th>Address2:</th>
            <td id='userinfo_addr2'></td>
        </tr>
        <tr>
            <th>Phone:</th>
            <td id='userinfo_phone'></td>
        </tr>
        <tr>
            <th>City:</th>
            <td id='userinfo_city'></td>
        </tr>
        <tr>
            <th>State:</th>
            <td id='userinfo_state'></td>
        </tr>
        <tr>
            <th>Zipcode:</th>
            <td id='userinfo_zipcode'></td>
        </tr>
        <tr>
            <th>Birthday:</th>
            <td id='userinfo_birthday'></td>
        </tr>
        <tr>
            <th>Gender:</th>
            <td id='userinfo_gender'></td>
        </tr>
        <tr>
            <th>Created:</th>
            <td id='userinfo_created'></td>
        </tr>
        <tr>
            <th>Cam:</th>
            <td id='userinfo_cam'></td>
        </tr>
        <tr>
            <th>Pub:</th>
            <td id='userinfo_pub'></td>
        </tr>
        <tr>
            <th>Pla:</th>
            <td id='userinfo_pla'></td>
        </tr>
        <tr>
            <th>Confirmed:</th>
            <td id='userinfo_confirmed'></td>
        </tr>
        <tr>
            <th>Last Visit:</th>
            <td id='userinfo_lv'></td>
        </tr>
        <tr>
            <th>Site:</th>
            <td id='userinfo_site'></td>
        </tr>
        <tr>
            <th>Credit:</th>
            <td id='userinfo_credit'></td>
        </tr>
        <tr>
            <th>Played:</th>
            <td id='userinfo_played'></td>
        </tr>
        <tr>
            <th>Won:</th>
            <td id='userinfo_won'></td>
        </tr>
        <tr>
            <th>Balance:</th>
            <td id='userinfo_balance'></td>
        </tr>
        <tr>
            <th>Offer Signed:</th>
            <td id='userinfo_offer'></td>
        </tr>
        <tr>
        	<th>Rewards Info:</th>
        	<td>
        		<table style="text-align:center;" id="userrewards">
        		</table>
        	</td>
        </tr>
        <tr>
        	<th>Unsub:</th>
        	<td>
        		<input type="button" id="setUnsub" 
        			__field="<?php echo $field; ?>" 
        			__condition="<?php echo isset($condition) ? $condition : ''; ?>" 
        			__userid="" />
        	</td>
        </tr>
        <tr>
        	<th>Set Password:</th>
        	<td>
        		<input type="text" value="" name="password" id="setpasstext" />
        			
        		<input type="button" value="Set" id="setPassword" 
        			__field="<?php echo $field; ?>" 
        			__condition="<?php echo isset($condition) ? $condition : ''; ?>" 
        			__userid="" /><br />
        		<label id="setPasswordlabel" style="color:red;"></label>
        	</td>
        </tr>
        <tr>
        	<th>Reward Credits</th>
        	<td>
        		<input type="text" value="" name="rewardcredits" id="rewardcreditstext" />
        			
        		<input type="button" value="Reward" id="rewardCredisBtn" 
        			__userid="" /><br />
        		<label id="rewardsCreditslabel" style="color:red;"></label>
        	</td>
        </tr>
        <tr>
        	<th>Send Confirm email:</th>
        	<td>
        		<input type="button" value="Send" id="sendConfirmEmail" 
        			__userid="" /><br />
        		<label id="sendConfirmEmaillabel" style="color:red;"></label>
        	</td>
        </tr>
    </table>
</div>
<script type="text/javascript">
	$('#userinfo').dialog({
		autoOpen: false,
		height: 'auto',
		width: 'auto',
		modal: true,
		buttons: {OK: function(){
						$(this).dialog( "close" );
					  }
				 }
	});

	$(".un").click(function(){
		$("#setpasstext").val('');
		$("#setPasswordlabel").html('');
		$.ajax({
			type: 'GET',
			url: "<?echo url('user/getuserinfo'); ?>",
			data: {
					uid: $(this).html()
				  },
			success: function(data){
						data = eval('('+data+')');
						var userinfo = data['userinfo'];
						var user_rewards = data['user_rewards'];

						$("#userinfo_name").html(userinfo[0]['firstname'] + ' &nbsp; ' + userinfo[0]['lastname']);
						$("#userinfo_email").html(userinfo[0]['email']);
						$("#userinfo_addr1").html(userinfo[0]['address1']);
						$("#userinfo_addr2").html(userinfo[0]['address2']);
						$("#userinfo_phone").html(userinfo[0]['phone']);
						$("#userinfo_city").html(userinfo[0]['city']);
						$("#userinfo_state").html(userinfo[0]['state']);
						$("#userinfo_zipcode").html(userinfo[0]['zipcode']);
						$("#userinfo_birthday").html(userinfo[0]['birthday']);
						$("#userinfo_gender").html( (userinfo[0]['gender']=='m' || userinfo[0]['gender']=='M') ? 'Male':'Female' );
						$("#userinfo_created").html(userinfo[0]['created']);
						$("#userinfo_lv").html(userinfo[0]['last_visit']);
						$("#userinfo_site").html(userinfo[0]['domain']);
						$("#userinfo_credit").html(userinfo[0]['credit']);
						$("#userinfo_played").html(userinfo[0]['played']);
						$("#userinfo_won").html("$"+userinfo[0]['won']);
						$("#userinfo_balance").html("$"+userinfo[0]['balance']);
						$("#userinfo_offer").html(userinfo[0]['offer']);
						$("#userinfo_cam").html(userinfo[0]['cam']);
						$("#userinfo_pub").html(userinfo[0]['pub']);
						$("#userinfo_pla").html(userinfo[0]['pla']);
						$("#userinfo_confirmed").html(userinfo[0]['confirmed']);

						$("#setUnsub").val( (userinfo[0]['unsub']=='1')? 'Sub':'Unsub' );
						$("#setUnsub").attr('__userid', userinfo[0]['id']);
						$("#setPassword").attr('__userid', userinfo[0]['id']);
						$("#sendConfirmEmail").attr('__userid', userinfo[0]['id']);
						$("#rewardCredisBtn").attr('__userid', userinfo[0]['id']);

						$("#userrewards").html("<tr><th>Time&nbsp;&nbsp;<\/th><th>Name&nbsp;&nbsp;<\/th><th>Credit&nbsp;&nbsp;<\/th><\/tr>");
						for(var index in user_rewards){
							var rewardstr = "<tr>";
							for(var v in user_rewards[index]){
								rewardstr += ("<td>"+user_rewards[index][v]+"</td>");
							}
							rewardstr += "</tr>";
							$("#userrewards").append(rewardstr);
						}
						$('#userinfo').dialog('open');
					 }
		});
	});

	$("#setUnsub").live('click', function(){
		var btn = $(this);
		$.ajax({
			type: 'POST',
			url: "<?echo url('user/setunsub'); ?>",
			data: {
					field: $(this).attr("__field"),
					condition: $(this).attr("__condition"),
					id: $(this).attr("__userid"),
					unsub: ($(this).val()=='Unsub')? '0' : '1'
				  },
			success: function(data){
						btn.val((data==0)? 'Unsub' : 'Sub');
					 }
		});
	});

	$("#setPassword").live('click', function(){
		var btn = $(this);
		$.ajax({
			type: 'POST',
			url: "<?echo url('user/setpassword'); ?>",
			data: {
					field: $(this).attr("__field"),
					condition: $(this).attr("__condition"),
					id: $(this).attr("__userid"),
					password: $("#setpasstext").val()
				  },
			success: function(data){
						if(data=='OK'){
							$("#setPasswordlabel").html('OK!');
							$('#userinfo').dialog('close');
						}else{
							$("#setPasswordlabel").html('Invalidate password!');
						}
					 }
		});
	});

	$('#sendConfirmEmail').live('click', function(){
		var btn = $(this);
		$.ajax({
			type: 'POST',
			url: "<?php echo url('user/sendConfirmEmail'); ?>",
			data: {
					uid: btn.attr('__userid')
				  },
			success: function(data){
						if(data == 'error'){
							$("#sendConfirmEmaillabel").html('Sent failed!');
						}else{
							$("#sendConfirmEmaillabel").html('confirm email has been sent');
						}
					 }
		});
	});

	$('#rewardCredisBtn').click(function(){
		var btn = $(this);
		var credits = $('#rewardcreditstext').val();
		if(isNaN(credits) || credits<0 || credits=='' ){
			$("#rewardsCreditslabel").html('credits should be numeric!');
			return false;
		}
		$.ajax({
			type: 'POST',
			url: "<?php echo url('user/rewardCredits'); ?>",
			data: {
					uid: btn.attr('__userid'),
					credits: credits
				  },
			success: function(data){
						if(data=='OK'){
							$("#rewardsCreditslabel").html('OK!');
							$("#userinfo_credit").html( $("#userinfo_credit").html()*1 + credits*1 );
						}else{
							$("#rewardsCreditslabel").html('Failed!');
						}
					 }
		});
	});
</script>
<?php $this->_endblock(); ?>